import hilog from '@ohos.hilog';

const TAG_INDEX = 0x1000

/**
 * 日志工具类
 */
class Log {
  private globalTag = 'BuyWhat'

  setTag(tag: string) {
    this.globalTag = tag
  }

  info(tag: string, ...args: any[]) {
    hilog.info(TAG_INDEX, this.globalTag, `[${tag}] ` + this.getFormat(args.length), args)
  }

  warn(tag: string, ...args: any[]) {
    hilog.warn(TAG_INDEX, this.globalTag, `[${tag}] ` + this.getFormat(args.length), args)
  }

  error(tag: string, ...args: any[]) {
    hilog.error(TAG_INDEX, this.globalTag, `[${tag}] ` + this.getFormat(args.length), args)
  }

  /**
   * 获取格式字符串
   * @param argLength args长度
   * @returns 返回长度数的 %{public}s...字符串
   */
  private getFormat(argLength): string {
    return new Array(argLength)
      .fill('%{public}s ') // 替换数组中所有元素
      .join('') // 数组拼接成字符串
      .replace(/\s$/g, '') // 去掉字符串最后的空格
  }
}

const log = new Log()

export default log